import { instance } from '../../utils/server.js';
import { authUrl } from '../apiUrl/auth';

/**
 * 登录
 */
export const login = (params) => {
  return new Promise((resolve, reject) => {
    instance
      .post(authUrl.login, params, {
        headers: {
          'Content-Type': 'application/json;charset=UTF-8',
        },
      })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

/**
 * 退出登录
 */
export const logout = () => {
  return new Promise((resolve, reject) => {
    instance
      .get(authUrl.logout)
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

/**
 * 获取用户信息
 */
export const getUserInfo = () => {
  return new Promise((resolve, reject) => {
    instance
      .get(authUrl.getUserInfo)
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

/**
 * 刷新token
 */
export const refreshToken = (params) => {
  return new Promise((resolve, reject) => {
    instance.post(authUrl.refreshToken);
  });
};

/**
 * 企业微信验证
 */
export const enterpriseWeChatVerification = () => {
  return new Promise((resolve, reject) => {
    instance.get(authUrl.VerifyURL);
  });
};

/**
 * 注册
 */
export const register = (params) => {
  return new Promise((resolve, reject) => {
    instance
      .post(authUrl.register, params, {
        headers: {
          'Content-Type': 'application/json;charset=UTF-8',
        },
      })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

/**
 * 获取三方登录回调地址
 */
export const querySocialAuthRedirect = (params) => {
  return new Promise((resolve, reject) => {
    instance
      .get(authUrl.socialAuthRedirect, {
        headers: {
          'Content-Type': 'application/json;charset=UTF-8',
        },
        params
      })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

/**
 * 社交账号授权
 */
export const SocialAccountAuthorization = (params) => {
  return new Promise((resolve, reject) => {
    instance
      .get(
        authUrl.socialLogin +
        `?code=${params.code}&type=${params.type}&scope=${params.scope}&state=${params.state}`,
        {
          headers: {
            'Content-Type': 'application/json;charset=UTF-8',
          },
        },
      )
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

/**
 *Google人机验证
 * @returns
 */
export function googleRecaptchaVerify(params) {
  console.log(params, 'params');
  return instance.post(authUrl.googleRecaptchaVerify, params, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  });
}

/**
 *  获取邮箱验证码
 * @param {*} params
 * @returns
 */
export const getMailVerificationCode = (params) => {
  return new Promise((resolve, reject) => {
    instance
      .get(authUrl.sendMailVerificationCode + `?username=${params}`)
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

/**
 * 获取密码重置验证码
 * @param {*} params
 * @returns
 */
export const getMailRetrieveVerificationCode = (params) => {
  return new Promise((resolve, reject) => {
    instance
      .get(authUrl.sendMailRetrieveVerificationCode + `?username=${params}`)
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

/**
 * 密码重置验证
 * @param {*} params
 * @returns
 */
export const resetPassword = (params) => {
  return new Promise((resolve, reject) => {
    instance
      .post(authUrl.resetPassword, params, {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
      })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};
